/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import { ArkMethod, ArkFile, LineColPosition } from "arkanalyzer/lib";
import Logger, { LOG_MODULE_TYPE } from 'arkanalyzer/lib/utils/logger';
import { BaseChecker, BaseMetaData } from "../BaseChecker";
import { Defects, IssueReport } from "../../model/Defects";
import { ClassMatcher, FileMatcher, MatcherCallback, MatcherTypes, MethodMatcher} from "../../matcher/Matchers";
import { Rule } from "../../model/Rule";
import { RuleListUtil } from "../../utils/common/DefectsList";
const defaultOptions: Options = {
    lib: 'always',
    path: 'never',
    types: 'prefer-import'
}
interface Options {
    lib?: 'always' | 'never';
    path?: 'always' | 'never';
    types?: 'always' | 'never' | 'prefer-import';
}
type importInfo = {
    declaringArkFile: ArkFile,
    importClauseName: string,
    importFrom: string,
    importType: string,
    lazyExportInfo: string,
    modifiers: number,
    nameBeforeAs: string,
    originTsPosition: LineColPosition
}
const logger = Logger.getLogger(LOG_MODULE_TYPE.HOMECHECK, 'TripleSlashReferenceCheck');
const gmetaData: BaseMetaData = {
    severity: 2,
    ruleDocPath: "docs/triple-slash-reference-check.md", // TODO: support url
    description: "Do not use a triple slash reference for code, use `import` style instead."
};
export class TripleSlashReferenceCheck implements BaseChecker {
    readonly metaData: BaseMetaData = gmetaData;
    public rule: Rule;
    public defects: Defects[] = [];
    public issues: IssueReport[] = [];
    private fileMatcher: FileMatcher = {
        matcherType: MatcherTypes.FILE,
    };
    private classMatcher: ClassMatcher = {
        file: [this.fileMatcher],
        matcherType: MatcherTypes.CLASS
    };
    private methodMatcher: MethodMatcher = {
        matcherType: MatcherTypes.METHOD,
        class: [this.classMatcher]
    };
    public registerMatchers(): MatcherCallback[] {
        const matchBuildCb: MatcherCallback = {
            matcher: this.methodMatcher,
            callback: this.check
        }
        return [matchBuildCb];
    }
    public check = (method: ArkMethod) => {
        // 将规则选项转换为Options类型
        let options: Options;
        if (this.rule && this.rule.option.length > 0) {
            options = this.rule.option[0] as Options;
        } else {
            options = defaultOptions;
        }
        if (method instanceof ArkMethod) {
            let declareClass = method.getDeclaringArkClass()
            const arkFile = declareClass.getDeclaringArkFile();
            const code = arkFile.getCode() ?? '';
            if (this.tripleSlash(code)) {
                //lib选项配置为always的情况,path和types配置为never的情况
                if (options.lib == 'always' && options.types == 'never' && options.path == 'never') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.libAlwaysDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib选项配置为always的情况
                if (options.lib == 'always' && !options.path && !options.types) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib配置为never,types配置为never的情况
                else if (!options.path && options.types == 'never' && options.lib == 'never') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.DoubleSlash(matched[i])) {
                                        continue;
                                    }
                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                        continue;
                                    }
                                    const target = this.extractCode(matched[i]);
                                    if (target) {
                                        this.addIssueReport(arkFile, matched[i], target);
                                    }
                                }
                            }
                        }
                    }
                }
                //lib配置为never,types配置为always的情况
                if (!options.path && options.types == 'always' && options.lib == 'never') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.typesAlwaysDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib配置为never,types配置为prefer-import的情况
                if (options.lib == 'never' && options.types == 'prefer-import' && !options.path) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.typesAlwaysDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib配置为always,types配置为never的情况
                if (!options.path && options.types == 'never' && options.lib == 'always') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.libAlwaysDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib配置为always,types配置为always的情况
                if (!options.path && options.types == 'always' && options.lib == 'always') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib配置为always,types配置为prefer-import的情况
                if (options.lib == 'always' && !options.path && options.types == 'prefer-import') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //path选项配置为always的情况,types和lib配置为never的情况
                if (options.path == 'always' && options.types == 'never' && options.lib == 'never') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathAlwaysDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //path选项配置为always的情况
                if (options.path == 'always' && !options.types && !options.lib) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathAlwaysDirectives(matched[i])) {
                                        const importInfos = arkFile.getImportInfos();
                                        if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                            if (this.DoubleSlash(matched[i])) {
                                                continue;
                                            }
                                            const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                            if (match) {
                                                const name = match[1];
                                                for (const importInfo of importInfos) {
                                                    const ImportInfo = importInfo as unknown as importInfo;
                                                    const importFrom = ImportInfo.importFrom;
                                                    if (importFrom == name) {
                                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                            continue;
                                                        }
                                                        const target = this.extractCode(matched[i]);
                                                        if (target) {
                                                            this.addIssueReport(arkFile, matched[i], target);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //path配置为never,types配置为always的情况
                if (options.path == 'never' && options.types == 'always' && !options.lib) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //path配置为never,types配置为never的情况
                if (options.path == 'never' && options.types == 'never' && !options.lib) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.libAlwaysDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //path配置为never,types配置为prefer-import的情况
                if (!options.lib && options.path == 'never' && options.types == 'prefer-import') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //path配置为always,types配置为always的情况
                if (options.path == 'always' && options.types == 'always' && !options.lib) { }
                //path配置为always,types配置为never的情况
                if (options.path == 'always' && options.types == 'never' && !options.lib) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //path配置为always,types配置为prefer-import的情况
                if (!options.lib && options.path == 'always' && options.types == 'prefer-import') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //path,types和lib都配置为never的情况
                if (options.path == 'never' && options.types == 'never' && options.lib == 'never') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.DoubleSlash(matched[i])) {
                                        continue;
                                    }
                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                        continue;
                                    }
                                    const target = this.extractCode(matched[i]);
                                    if (target) {
                                        this.addIssueReport(arkFile, matched[i], target);
                                    }
                                }
                            }
                        }
                    }
                }
                //path,types和lib都配置为always的情况
                if (options.path == 'always' && options.types == 'always' && options.lib == 'always') { }
                //path和lib配置为never的情况
                else if (!options.types && options.lib == 'never' && options.path == 'never') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.typesAlwaysDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //types选项配置为always的情况,path和lib配置为never的情况
                else if (options.types == 'always' && options.lib == 'never' && options.path == 'never') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.typesAlwaysDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //types选项配置为always的情况
                else if (options.types == 'always' && !options.lib && !options.path) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib和path选项配置为always的情况,types配置为never的情况
                else if (options.lib == 'always' && options.path == 'always' && options.types == 'never') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //types配置为never的情况
                else if (options.types == 'never' && !options.lib && !options.path) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.libAlwaysDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib配置为always,path配置为never的情况
                if (options.lib == 'always' && options.path == 'never' && !options.types) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib配置为never,path配置为always的情况
                if (options.lib == 'always' && options.path == 'never' && !options.types) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.libNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib和types选项配置为always,path配置为never的情况
                else if (options.lib == 'always' && options.types == 'always' && options.path == 'never') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //path配置为never的情况
                if (options.path == 'never' && !options.types && !options.lib) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //types和path选项配置为always,lib配置为never的情况
                if (options.path == 'always' && options.types == 'always' && options.lib == 'never') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.libNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib配置为never的情况
                if (options.lib == 'never' && !options.types && !options.path) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.typesAlwaysDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib,path配置为always
                if (options.lib == 'always' && options.path == 'always' && !options.types) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib,path配置为always,types配置为prefer-import的情况
                if (options.lib == 'always' && options.path == 'always' && options.types == 'prefer-import') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //lib配置为never,path配置为always,types配置为prefer-import的情况
                if (options.lib && options.lib === 'never' && options.path == 'always' && options.types == 'prefer-import') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.libNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //types,path和lib选项配置都配置为默认选项的情况
                if (options.lib == 'always' && options.path == 'never' && options.types == 'prefer-import') {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                //types配置为prefer-import的情况
                if (options.types == 'prefer-import' && !options.lib && !options.path) {
                    if (this.normalDirectives(code)) {
                        const matched = code.split('\r\n');
                        if (matched) {
                            for (let i = 0; i < matched.length; i++) {
                                if (this.tripleSlash(matched[i])) {
                                    if (this.pathNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                            continue;
                                        }
                                        const target = this.extractCode(matched[i]);
                                        if (target) {
                                            this.addIssueReport(arkFile, matched[i], target);
                                        }
                                    }
                                    const importInfos = arkFile.getImportInfos();
                                    if (importInfos.length > 0 && this.typesNeverDirectives(matched[i])) {
                                        if (this.DoubleSlash(matched[i])) {
                                            continue;
                                        }
                                        const match = matched[i].match(/types\s*=\s*"([^"]*)"/);
                                        if (match) {
                                            const name = match[1];
                                            for (const importInfo of importInfos) {
                                                const ImportInfo = importInfo as unknown as importInfo;
                                                const importFrom = ImportInfo.importFrom;
                                                if (importFrom == name) {
                                                    if (matched[i - 1] == '/*' && matched[i + 1] == '*/') {
                                                        continue;
                                                    }
                                                    const target = this.extractCode(matched[i]);
                                                    if (target) {
                                                        this.addIssueReport(arkFile, matched[i], target);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    private extractCode(str: string) {
        const regex = /="([^"]+)"/;
        const match = str.match(regex);
        if (match) {
            return match[1]; // 返回捕获的第一个组，即双引号之间的内容
        } else {
            return null; // 如果没有匹配到，返回 null
        }
    }
    //判断给定的字符串中是否包含三个斜杠注释。
    private tripleSlash(text: string) {
        const regex = /\/\/\//;
        return regex.test(text);
    }
    private DoubleSlash(text: string) {
        const regex = new RegExp('\/\/.*?\/\/\/');
        return regex.test(text);
    }
    private normalDirectives(text: string) {
        return /\s*<reference\s+(lib|path|types)="[^"]*"\s*\/>/g.test(text);
    }
    private libAlwaysDirectives(text: string) {
        return /\s*<reference\s+(path|types)="[^"]*"\s*\/>/g.test(text);
    }
    private typesAlwaysDirectives(text: string) {
        return /\s*<reference\s+(path|lib)="[^"]*"\s*\/>/g.test(text);
    }
    private pathAlwaysDirectives(text: string) {
        return /\s*<reference\s+(lib|types)="[^"]*"\s*\/>/g.test(text);
    }
    private pathNeverDirectives(text: string) {
        return /\s*<reference\s+(path)="[^"]*"\s*\/>/g.test(text);
    }
    private libNeverDirectives(text: string) {
        return /\s*<reference\s+(lib)="[^"]*"\s*\/>/g.test(text);
    }
    private typesNeverDirectives(text: string) {
        return /\s*<reference\s+(types)="[^"]*"\s*\/>/g.test(text);
    }
    private addIssueReport(arkFile: ArkFile, name: string, target?: string) {
        this.metaData.description = `Do not use a triple slash reference for ${target}, use "import" style instead.`;
        const severity = this.rule.alert ?? this.metaData.severity;
        const warnInfo = this.getLineAndColumn(arkFile, name);
        this.metaData.description = this.metaData.description.padEnd(130);
        const filePath = arkFile.getFilePath();
        let defects = new Defects(warnInfo.line, warnInfo.startCol, warnInfo.endCol, this.metaData.description, severity, this.rule.ruleId, filePath,
            this.metaData.ruleDocPath, true, false, false);
        this.issues.push(new IssueReport(defects, undefined));
        RuleListUtil.push(defects);
    }
    private getLineAndColumn(arkfile: ArkFile, name: string) {
        if (arkfile) {
            const code = arkfile.getCode() ?? '';
            const lines = code.split('\r\n');
            let lineNumber = 1;
            for (const line of lines) {
                const lineIndex = line.indexOf(name);
                if (lineIndex !== -1) {
                    const startCol = lineIndex + 1; // 列号从1开始
                    const endCol = lineIndex + 1;
                    const originPath = arkfile.getFilePath();
                    return { line: lineNumber, startCol, endCol, filePath: originPath }
                }
                lineNumber++;
            }
        } else {
            logger.debug('originStmt or arkFile is null');
        }
        return { line: -1, startCol: -1, endCol: -1, filePath: '' };
    }
}